import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { loanRecycleStatisticsAction } from './index';
import { CommonTable } from 'components';
import SearchList from './SearchList/SearchList';
import moment from "moment/moment";

const convertParams = (time) => {
    const isArr = Array.isArray(time) && time.length > 0;
    return {
        startTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00') : '',
        endTime: isArr ? time[1].format('YYYY-MM-DD 23:59:59') : ''
    };
}

class LoanRecycleStatistics extends Component{
    columns = [
        { title: '放款时间', dataIndex: 'day', key: 'day' },
        { title: '放款笔数', dataIndex: 'loanNum', key: 'loanNum' },
        { title: '放款本金', dataIndex: 'lendMoney', key: 'lendMoney' },
        { title: '应还金额', dataIndex: 'deviceMoney', key: 'deviceMoney' },
        { title: '正常还款', dataIndex: 'normalRepayment', key: 'normalRepayment' },
        // { title: '正常展期费用', dataIndex: 'normalLeng', key: 'normalLeng' },
        { title: '逾期还款', dataIndex: 'overdueRepayment', key: 'overdueRepayment' },
        // { title: '逾期展期费用', dataIndex: 'overdueLeng', key: 'overdueLeng' },
        { title: '逾期滞纳金', dataIndex: 'overMoney', key: 'overMoney' },
        // { title: '在贷金额', dataIndex: 'ingLoan', key: 'ingLoan' },
        { title: '正常在贷金额', dataIndex: 'ingLoanNormal', key: 'ingLoanNormal' },
        { title: '逾期在贷金额', dataIndex: 'ingLoanDue', key: 'ingLoanDue' },
        { title: '总回款', dataIndex: 'totalRepayment', key: 'totalRepayment' },
        {
            title: '首逾率',
            dataIndex: 'firstDueLoanRate',
            key: 'firstDueLoanRate',
            render(text) {
                const str = Number(text) * 100;

                return Number.isInteger(str) ? `${str}%` : `${str.toFixed(2)}%`;
            }
        },
        {
            title: '本金回收率',
            dataIndex: 'rate',
            key: 'rate',
            render(text) {
                const str = Number(text) * 100;

                return Number.isInteger(str) ? `${str}%` : `${str.toFixed(2)}%`;
            }
        },
    ];
    constructor(props) {
        super(props);
        this.state = {
            time: [moment().subtract(7, 'days'), moment()]
        };
    }

    componentDidMount() {
        const { getTableData } = this.props;
        const params = convertParams(this.state.time);
        getTableData({ ...params });
    }

    handleSearch = (obj) => {
        const { time = [] } = obj;
        const { getTableData } = this.props;
        const params = convertParams(time);
        getTableData({ ...params });
    }
    render() {
        const { tableData: { data }, loading } = this.props;
        return (
            <div>
                <SearchList handleSearch={this.handleSearch} initTime={this.state.time}/>
                <CommonTable
                    columns={this.columns}
                    dataSource={data}
                    loading={loading}
                    pagination={null}
                />
            </div>
        );
    }
}

const mapStateToProps = (state) => {
    const { businessStatisticsState: { loanRecycleStatisticsState } } = state;
    return {
        tableData: loanRecycleStatisticsState['tableData'],
        loading: loanRecycleStatisticsState['loading']
    };
};
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: loanRecycleStatisticsAction.lrsGetTableData,
        setTableData: loanRecycleStatisticsAction.lrsSetTableData
    }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(LoanRecycleStatistics);